import * as userModel from '../../models/user';
import config, { NODE_ENV } from '../../config';
import cacheHelp from '../../helps/cache';
import * as ValidHelp from '../../helps/valid';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* validToken({ token }) {
  if (!token) throw ValidException('TOKEN_EMPTY');
  let user = yield userModel.getByToken(token);
  if (!user) throw EmptyException('TOKEN_INVALID');
  if (new Date(user.expire_time) < new Date()) throw ValidException('Token过期, 请刷新token');
  yield user.loginSave();
  return user;
}

export function* refreshToken({ refresh_token, user_id }) {
  if (!refresh_token) throw ValidException('Token empty');
  let user = yield userModel.get(user_id);
  if (!user) throw EmptyException('User not exist');
  if (user.refresh_token !== refresh_token) throw ValidException('Token invalid');
  yield user.refreshToken(config.cache.token_expire_seconds);
  return user;
}

export function* register({ mobile, password, mobile_code, verification_code }) {
  if (!ValidHelp.Mobile(mobile)) throw ValidException('手机号错误');
  if (!ValidHelp.Required(password)) throw ValidException('密码不能为空');
  if (NODE_ENV !== 'test') {
    let result = yield cacheHelp.get(['mobile', mobile, { code: mobile_code }]);
    if (!result) throw ValidException('验证错误');
    if (result.valid_code !== verification_code) throw ValidException('验证错误');
  }
  let user = yield userModel.getByMobile(mobile);
  if (user) throw ValidException('手机号已注册');
  user = yield userModel.add({ username: '', password, avatar: '', mobile, nickname: '', wechat: '' });
  yield cacheHelp.clear(['mobile', mobile, { code: mobile_code }]);
  if (!user) throw SystemException('创建用户失败');
  yield user.refreshToken(config.cache.token_expire_seconds);
  return user.info();
}

export function* login({ mobile, password }) {
  if (!ValidHelp.Mobile(mobile)) throw ValidException('手机号错误');
  if (!ValidHelp.Required(password)) throw ValidException('密码不能为空');
  let user = yield userModel.getByMobile(mobile);
  if (!user) throw EmptyException('用户不存在');
  if (!user.validatePassword(password)) throw ValidException('密码错误');
  yield user.refreshToken(config.cache.token_expire_seconds);
  return user.info();
}

export function* resetPassword({ mobile, password, mobile_code, verification_code }) {
  if (!ValidHelp.Mobile(mobile)) throw ValidException('手机号错误');
  if (!ValidHelp.Required(password)) throw ValidException('密码不能为空');
  if (NODE_ENV !== 'test') {
    let result = yield cacheHelp.get(['mobile', mobile, { code: mobile_code }]);
    if (!result) throw ValidException('验证错误');
    if (result.valid_code !== verification_code) throw ValidException('验证错误');
  }
  let user = yield userModel.getByMobile(mobile);
  if (!user) throw ValidException('该手机号未注册');
  yield cacheHelp.clear(['mobile', mobile, { code: mobile_code }]);
  yield user.changePassword(password);
  return {};
}

export function* changePassword({ old_password, new_password, user_id }) {
  if (!ValidHelp.Required(old_password)) throw ValidException('旧密码不能为空');
  if (!ValidHelp.Required(new_password)) throw ValidException('新密码不能为空');
  let user = yield userModel.get(user_id);
  if (!user) throw EmptyException('用户不存在');
  if (!user.validatePassword(old_password)) throw ValidException('密码错误');
  yield user.changePassword(new_password);
  return {};
}

export function* getInfo({ user_id }) {
  if (!ValidHelp.Required(user_id)) throw ValidException('用户不能为空');
  let user = yield userModel.get(user_id);
  if (!user) throw EmptyException('用户不存在');
  return user.info();
}

export function* putInfo({ user_id, username, avatar, nickname, wechat, type }) {
  if (!ValidHelp.Required(user_id)) throw ValidException('用户不能为空');
  let user = yield userModel.get(user_id);
  if (!user) throw EmptyException('用户不存在');
  yield user.put({ username, password: null, avatar, area: '', mobile: null, nickname, wechat, type });
  return user.info();
}
